﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using UpdatePriceEbay.eBayService;
using System.Data;
using System.Threading;
using System.IO;
using System.Runtime.InteropServices;
using System.Data.SqlClient;

namespace UpdatePriceEbay
{
    class PretLinkClass
    {
        public double pretMinim;
        public string link;

        public PretLinkClass(double pretMinim, string link)
        {
            this.pretMinim = pretMinim;
            this.link = link;
        }
    }

    class UpdateProdusClass
    {
        static public PretLinkClass CautaProdus(string type, string Value)
        {
            using (FindingServicePortTypeClient client = new FindingServicePortTypeClient())
            {
                MessageHeader header = MessageHeader.CreateHeader("CustomHeader", "", "");
                using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
                {

                    OperationContext.Current.OutgoingMessageHeaders.Add(header);

                    HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();

                    httpRequestProperty.Headers.Add("X-EBAY-SOA-SECURITY-APPNAME", "FilipSin-b22f-4d3d-90c6-68eb75bf2db2");  // Acest ultim argument l-am luat de pe contul de eBay Developer de la Application Settings

                    httpRequestProperty.Headers.Add("X-EBAY-SOA-OPERATION-NAME", "findItemsByProduct");

                    httpRequestProperty.Headers.Add("X-EBAY-SOA-GLOBAL-ID", "EBAY-US");

                    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;

                    FindItemsByProductRequest request = new FindItemsByProductRequest();
                    request.productId = new ProductId();
                    request.productId.type = type;
                    request.productId.Value = Value;

                    ItemFilter filtru = new ItemFilter();
                    filtru.name = ItemFilterType.Condition;
                    filtru.value = new string[1];
                    filtru.value[0] = "New";

                    request.itemFilter = new ItemFilter[1];
                    request.itemFilter[0] = filtru;
                    
                    FindItemsByProductResponse response = client.findItemsByProduct(request);

                    int numarProduse = 0;

                    String linkProdusPretMinim = "";
                    double pretMinim = -1;
                    try
                    {
                        foreach (var item in response.searchResult.item)
                        {
                            // Daca avem un singur produs acesta va fi cel cu pret minim
                            if (numarProduse == 0)
                            {
                                pretMinim = item.sellingStatus.currentPrice.Value;
                                linkProdusPretMinim = item.viewItemURL;
                                numarProduse++;
                            }
                            // Daca sunt mai multe produse va trebui determint pretul minim
                            else if (pretMinim > item.sellingStatus.currentPrice.Value)
                                 {
                                     pretMinim = item.sellingStatus.currentPrice.Value;
                                     linkProdusPretMinim = item.viewItemURL;
                                 }                                                       
                        }
                    }
                    catch (Exception except)
                    {
                        return new PretLinkClass(-1, null);
                    }

                    // Daca pretul minim a ramas -1 inseamna ca n s-au gasit produse pentru valoarea curenta a ISBN-ului sau UPC-ului

                    // Va trebui sa verific in functie ca pretMinm sa fie > 0
                    return new PretLinkClass(pretMinim, linkProdusPretMinim);                    
                }
            }
        }
    }
}
